define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiluhouse/current/advertise/index' + location.search,
                    add_url: 'xiluhouse/current/advertise/add',
                    edit_url: 'xiluhouse/current/advertise/edit',
                    del_url: 'xiluhouse/current/advertise/del',
                    multi_url: 'xiluhouse/current/advertise/multi',
                    insidepages_url: 'xiluhouse/current/insidepages/index',
                    table: 'advert',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),sortable:true},
                        {field: 'name', title: __('Name'),operate:'LIKE'},
                        {field: 'position', title: __('Position'), searchList: {"1":__('Position 1'),"2":__('Position 2'),"3":__('Position 3'),"4":__('Position 4'),"5":__('Position 5')}, formatter: Table.api.formatter.normal},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate:false},
                        
                        { field: 'goto_type', title: __('Goto_type'), searchList: { "1": __('Goto_type 1'), "2": __('Goto_type 2'), "3": __('Goto_type 3'), "4": __('Goto_type 4') , "5": __('Goto_type 5') }, formatter: Table.api.formatter.normal },
                        
                        // {field: 'goto_param', title: __('Goto_param')},
                        // {field: 'inside_pages_id', title: __('Inside_pages_id')},
                        {field: 'goto_url', title: __('跳转链接'),operate:false,formatter: function (value, row, index) {
                            if(row.goto_type==3){
                                var html='';
                                html += '<div>' + __('insidepages.name') + ':' + row.insidepages.name + '</div>';
                                if (row.goto_param) {
                                    html+='<div>'+__('goto_param')+':'+row.goto_param+'</div>';
                                }
                                return html;
                            }else if(row.goto_type==2){
                                return row.goto_url;
                            }else{
                                return '-';
                            }
                        }},
                        //{field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'),operate:false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        // {field: 'insidepages.name', title: __('Insidepages.name')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiluhouse/current/advertise/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiluhouse/current/advertise/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiluhouse/current/advertise/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on('click', '.position_check', function () {
                    var position = $(this).val();
                    switch (position) {
                        case '1':
                            $('#recommend_size').html('300*350px');
                            break;
                        case '2':
                            $('#recommend_size').html('335*110px');
                            break;
                        case '3':
                            $('#recommend_size').html('335*64px');
                            break;
                        case '4':
                            $('#recommend_size').html('335*90px');
                            break;
                        case '5':
                            $('#recommend_size').html('335*90px');
                            break;
                        case '6':
                            $('#recommend_size').html('335*81px');
                            break;
                        case '7':
                            $('#recommend_size').html('335*120px');
                            break;
                    }
                })
                $(document).on('click', '.goto_type_check', function () {
                    var goto_type = $(this).val();
                    switch (goto_type) {
                        case '1':
                            $('.goto_param').hide();
                            $('.inside_pages_id').hide();
                            $('.goto_url').hide();
                            break;
                        case '2':
                            $('.goto_param').hide();
                            $('.inside_pages_id').hide();
                            $('.goto_url').show();
                            break;
                        case '3':
                            $('.goto_param').show();
                            $('.inside_pages_id').show();
                            $('.goto_url').hide();
                            break;
                        case '4':
                            $('.goto_param').hide();
                            $('.inside_pages_id').hide();
                            $('.goto_url').hide();
                            break;
                        case '5':
                            $('.goto_param').hide();
                            $('.inside_pages_id').hide();
                            $('.goto_url').show();
                            break;
                    }
                })
                

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});